﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.Text;

namespace FilStartUps.Forum
{
    public partial class Thread : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetForumThread(Request.QueryString["t"].Trim());
            }
        }

        private void GetForumThread(string id)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                int forumidFK = int.Parse(id);
                var forum_ = db_connect.Forums.Where(f => f.forumidPK.Equals(forumidFK)).SingleOrDefault();

                //(from f in db_connect.Forums
                //          where f.forumidPK.Equals(forumidFK)
                //          select f).SingleOrDefault();

                lbforum.Text = forum_.forumtitle;

                var forum = (from f in db_connect.ForumThreads
                             join m in db_connect.Members
                             on f.memberidFK equals m.memberidPK
                             where f.forumidFK.Equals(forumidFK)
                             && f.disabled.Equals(false)
                             let cCount =
                             (from fm in db_connect.ForumMessages
                              where fm.forumthreadidFK.Equals(f.forumthreadidPK)
                              select fm).Count()
                             select new
                             {
                                 membername = m.emailaddress,
                                 views = f.views,
                                 datecreated = f.datecreated,
                                 threadtitle = f.threadtitle,
                                 forumthreadidPK = f.forumthreadidPK,
                                 totalpost = cCount
                             }).ToList();
                if (forum.Count() > 0)
                {
                    rpForumThread.DataSource = forum;
                    rpForumThread.DataBind();
                }
                else
                {

                    lberror.Text = "The forum is empty, no topics have been created yet.";
                    lberror.Visible = true;
                }
            }
        }


        protected void lbnewthread_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Forum/NewThread.aspx?t=" + Request.QueryString["t"].Trim());
        }

        protected void rpForumThread_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                UpdateViewsThread(Convert.ToInt32(e.CommandArgument));
                Response.Redirect("~/Forum/ShowMessage.aspx?t=" + e.CommandArgument);
            }
        }

        private void UpdateViewsThread(int threadidPK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var thread = db_connect.ForumThreads.Where(f => f.forumthreadidPK.Equals(threadidPK)).SingleOrDefault();
                thread.views += 1;
                db_connect.SaveChanges();
            }
        }


        public IEnumerable<Forum_Thread> GetData()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                int forumidFK = Convert.ToInt32(Request.QueryString["t"].Trim());

                var forum_ = (from f in db_connect.Forums
                              where f.forumidPK.Equals(forumidFK)
                              select f).SingleOrDefault();
                lbforum.Text = forum_.forumtitle;

                var forum = (from f in db_connect.ForumThreads
                             where f.forumidFK.Equals(forumidFK)
                             && f.disabled.Equals(false)
                             let cCount =
                             (from fm in db_connect.ForumMessages
                              where fm.forumthreadidFK.Equals(f.forumthreadidPK)
                              select fm).Count()
                             select new Forum_Thread
                             {
                                 views = f.views,
                                 datecreated = f.datecreated,
                                 threadtitle = f.threadtitle,
                                 forumthreadidPK = f.forumthreadidPK,
                                 totalpost = cCount
                             }).ToList();

                return forum;
            }
        }


    }
}